function time_is_widget() {
    var ca = 0, tD = 0, tout = 0, updint = 1000, tl = "", U = "undefined", i, j, rd
    var p = { n: ["%E6%98%9F%E6%9C%9F%E6%97%A5.%E6%98%9F%E6%9C%9F%E4%B8%80.%E6%98%9F%E6%9C%9F%E4%BA%8C.%E6%98%9F%E6%9C%9F%E4%B8%89.%E6%98%9F%E6%9C%9F%E5%9B%9B.%E6%98%9F%E6%9C%9F%E4%BA%94.%E6%98%9F%E6%9C%9F%E5%85%AD.%E5%91%A8%E6%97%A5.%E5%91%A8%E4%B8%80.%E5%91%A8%E4%BA%8C.%E5%91%A8%E4%B8%89.%E5%91%A8%E5%9B%9B.%E5%91%A8%E4%BA%94.%E5%91%A8%E5%85%AD.1%E6%9C%88.2%E6%9C%88.3%E6%9C%88.4%E6%9C%88.5%E6%9C%88.6%E6%9C%88.7%E6%9C%88.8%E6%9C%88.9%E6%9C%88.10%E6%9C%88.11%E6%9C%88.12%E6%9C%88"], w: "%E5%91%A8 ", W: "%E5%91%A8 .n", dy: " %E5%A4%A9" }
    for (i in p) p[i] = decodeURIComponent(p[i])
    p["n"] = p["n"].split(".")
    this.init = function (P) {
        if (tout != 0) clearInterval(time_is_widget.tout)
        var a, q = [], c = { dayname: "%l", dname: "%D", daynum: "%d", dnum: "%j", day_in_y: "%z", week: "%W", monthname: "%F", monthnum: "%m", mnum: "%n", yy: "%y", year: "%Y", "12hours": "%h", hours: "%H", minutes: "%i", seconds: "%s", AMPM: "%A" }, ct = "TIME", tF = "%H:%i:%s", dF = "%Y-%d-%m", sF = "%srH:%srm-%ssH:%ssm"
        for (i in P) {
            a = P[i]
            a["p"] = ""
            if (typeof a["id"] == U) a["id"] = i
            if (typeof a["time_format"] != U) tF = a["time_format"]
            if (typeof a["date_format"] != U) dF = a["date_format"]
            if (typeof a["sun_format"] != U) sF = a["sun_format"]
            for (j in c) {
                dF = dF.replace(j, c[j])
                tF = tF.replace(j, c[j])
            }
            if (typeof a["template"] != U) ct = a["template"]
            tl = "https://time.is/" + i.substr(0, a["id"].indexOf("AA")).replace("__", ",_")
            tl = "<span onclick=\"location='" + tl.replace("'", "\\\'") + "'\" title=\"" + tl + '">'
            a["template"] = ct.replace("SUN", tl + sF + "</span>").replace("TIME", tl + tF + "</span>").replace("DATE", '<span onclick="window.location=\'https://time.is/calendar\'" title="https://time.is/calendar">' + dF + '</span>')
            if (-1 == a["template"].indexOf("<sp")) a["template"] = '<span onclick="window.location=\'https://time.is/\'">' + a["template"] + "</span>"
            if (typeof a["v"] == U) {
                q.push(a["id"])
                if (typeof a["coords"] != U) q[q.length - 1] += "." + a["coords"].replace(",", "_")
            }
            P[i] = a
        }
        this.ca = P
        if (0 < q.length) {
            i = document.createElement("script")
            i.setAttribute("src", "//widget.time.is/?" + encodeURIComponent(q.join("..")) + "&t=" + new Date().getTime())
            j = document.getElementsByTagName("head").item(0)
            j.appendChild(i)
        } else this.tick()
    }
    this.cb = function (t, r, a) {
        var rpT = new Date(), n = 0
        time_is_widget.tD = rpT.getTime() - t - Math.round((rpT - r) / 2)
        for (i in this.ca) {
            this.ca[i]["v"] = a[n]
            n++
        }
        this.tick()
    }
    this.tick = function () {
        var tU = new Date(), t = new Date(), c, pw
        tU.setTime(tU.getTime() - this.tD)
        if (!rd && document.readyState === "complete") {
            if (document.getElementById) i = document.getElementById("time_is_link")
            else i = eval("time_is_link")
            if (null == i || i.href.indexOf("time.is/") === -1) {
                console.log('Link back to Time.is missing!')
                return ''
            } else {
                if (!i.rel || i.innerHTML.length < 3 || i.offsetWidth < 10 || i.offsetHeight < 5) {
                    rd = "//widget.time.is/r/?" + i.rel + ".w" + i.offsetWidth + ".h" + i.offsetHeight + "." + encodeURIComponent(i.innerHTML)
                    i = document.createElement("script")
                    i.setAttribute("src", rd)
                    j = document.getElementsByTagName("head").item(0)
                    j.appendChild(i)
                }
            }
        }
        for (i in this.ca) {
            c = this.ca[i]
            // 切换时区时会报错
            try {
                if (typeof c["v"][0] != U) {
                    if ((0 < c["v"][1]) && (c["v"][1] < tU.getTime())) {
                        c["v"][0] = c["v"][2]
                        c["v"][1] = 0
                    }
                    t.setTime(c["v"][0] * 60000 + tU.getTime())
                    var d, y = t.getUTCFullYear() + "", m = t.getUTCMonth() + 1, N = new Date(y, 0, 1), o = N.getDay() - 1
                    if (o == -1) o = 6
                    var W = Math.floor((t - N + N.getTimezoneOffset() * 60000) / 604800000 + (o / 7)), dn = p["dy"] + " " + Math.floor((t - N + N.getTimezoneOffset() * 60000) / 86400000 + 1)
                    if (o < 4) W++
                    if (W == 0) {
                        W = 52
                        if (new Date(y - 1, 0, 1).getDay() == 4 || new Date(y - 1, 11, 31).getDay() == 4) W = 53
                    }
                    if (p["W"] == "hy") {
                        if (W == 1) pw = "1-ին շաբաթ"
                        else pw = W + "-րդ շաբաթ"
                    } else pw = p["W"].replace(".n", W)
                    var g = { t: t.getUTCHours(), r: c["v"][3], s: c["v"][5] }, h = {}
                    for (j in g) {
                        h[j] = l0(g[j])
                        h[j + "H"] = h[j]
                        h[j + "M"] = "AM"
                        if (11 < h[j]) {
                            h[j + "M"] = "PM"
                            h[j] = l0(h[j] - 12)
                        }
                        if (h[j] == "00") h[j] = 12
                    }
                    d = c["template"].replace("srhour", h["rH"]).replace("sr12hour", h["r"]).replace("srAMPM", h["rM"]).replace("srminute", l0(c["v"][4])).replace("sshour", h["sH"]).replace("ss12hour", h["s"]).replace("ssAMPM", h["sM"]).replace("ssminute", l0(c["v"][6])).replace("dlhours", c["v"][7]).replace("dlminutes", c["v"][8]).replace("%h", h["t"]).replace("%H", h["tH"]).replace("%i", l0(t.getUTCMinutes())).replace("%s", l0(t.getUTCSeconds())).replace("%A", h["tM"]).replace("%j", t.getUTCDate()).replace("%d", l0(t.getUTCDate())).replace("%W", pw).replace("%n", m).replace("%m", l0(m)).replace("%y", y.substr(2, 2)).replace("%Y", y).replace("%F", p["n"][13 + m]).replace("%l", p["n"][t.getUTCDay()]).replace("%D", p["n"][7 + t.getUTCDay()]).replace("%z", dn)
                    j = d.indexOf(">")
                    d = d.substr(0, j + 1) + d.substr(j + 1, 1).toUpperCase() + d.substr(j + 2, d.length - 1)
                    if (d != c["p"]) {
                        if (document.getElementById) o = document.getElementById(i); else o = eval()
                        if (null != o) { o.innerHTML = d; c["p"] = d; handle(d) }
                    }
                }
                if (typeof c["callback"] != U) eval(c["callback"] + "(d)")
            } catch (e) { }
        }
        tout = setTimeout('time_is_widget.tick("")', updint - tU % updint)
    }
    function l0(n) { return n > 9 ? n : "0" + n }
}

function getTimer(v) {
    var reg = RegExp('<span.*?</span>', 'g')
    var lt = v.matchAll(reg);
    var index = 0;
    for (var [dom] of lt) {
        switch (index) {
            case 0: {
                var temp = dom.match(/>.*</)[0].slice(1, -1).split(':');
                var hour = temp[0];
                var min = temp[1];
                var s = temp[2].slice(0, -2);
                var ampm = temp[2].slice(-2);
                if (ampm.toUpperCase() == "PM") hour = String(Number(hour) + 12)
                break;
            };
            case 1: {
                var temp = dom.match(/>.*</)[0].slice(1, -1);
                var iterator = temp.matchAll(/[0-9]+/g)
                var year = iterator.next().value[0];
                var month = iterator.next().value[0];
                var day = iterator.next().value[0];
                var week = iterator.next().value[0];
                var weekday = temp.match(/星期./)[0]
                break;
            }
            case 2: {
                var info = dom.match(/>.*</)[0].slice(1, -1).replace('<br>', ' ')
                break;
            }
        }
        index++;
    }
    return {
        hour, min, s, ampm, year, month, day, week, weekday,
        info
    }

}

const timerMap = [
    {
        id: "timer-hour",
        innerText: "hour"
    },
    {
        id: "timer-min",
        innerText: "min"
    },
    {
        id: "timer-s",
        innerText: "s"
    },
    {
        id: "timer-year",
        innerText: "year"
    },
    {
        id: "timer-month",
        innerText: "month"
    },
    {
        id: "timer-day",
        innerText: "day"
    },
    {
        id: "timer-week",
        innerText: "week"
    },
    {
        id: "timer-weekday",
        innerText: "weekday"
    }
]
timerMap.map(function (e) {
    e.dom = document.getElementById(e.id);
    return e;
})

function handle(v) {
    const timer = getTimer(v);
    for (var obj of timerMap) {
        if (obj.dom != null) {
            obj.dom.innerText = timer[obj.innerText];
        }
    }
}
// ---------------
var time_is_widget = new time_is_widget();
class Timer {
    static baseDom = null
    static template = ''
    static options = {
        city: 'Shenzhen_z43d',
        coords: "22.5455400,114.0683000"
    }
    static setTemplate() {
        Timer.template = '<a href="https://time.is/Shenzhen" id="time_is_link" rel="nofollow" style="font-size:36px"></a><span id="' + Timer.options.city + '" style="font-size:36px"></span>'
    }
    static switchZone(city, coords) {
        Timer.options = {
            city, coords
        }
        Timer.init();
    }
    static init() {
        var timer = document.getElementById('timer')
        if (timer) {
            if (!Timer.baseDom) {
                Timer.baseDom = document.createElement('div');
                Timer.baseDom.style.display = 'none';
                timer.appendChild(Timer.baseDom);
            }
            Timer.setTemplate();
            Timer.baseDom.innerHTML = Timer.template;
            var options = new Object();
            options[Timer.options.city] = {
                coords: Timer.options.coords,
                template: "TIME<br>DATE<br>SUN",
                time_format: "12hours:minutes:secondsAMPM",
                date_format: "year年monthnamednum日dayname，第week周",
                sun_format: "日出: srhour:srminute 日落: sshour:ssminute<br>昼长: dlhours时 dlminutes分"
            }
            time_is_widget.init(options);
        }
    }
}